package xyz.wjwnya.auth_module.service.impl;

import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;
import xyz.wjwnya.auth_module.service.AuthService;
import xyz.wjwnya.database.entity.User;
import xyz.wjwnya.database.mapper.UserMapper;

import java.util.ArrayList;
import java.util.List;

import static xyz.wjwnya.common.auth.UserType.*;

@Service
public class AuthServiceImpl implements AuthService {

    private final UserMapper userMapper;

    public AuthServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public UserData login(String account, String password, String userType) {
        System.out.println(userType);
        User user = userMapper.selectUserByAuth(account, password,userType);
        System.out.println(user);
        if (user == null) throw new UserNotFoundException(account, password, userType);
        return new UserData(
            user.getAccount(), user.getPassword(),
            user.getUserName(), userType
        );
    }

    @Override
    public List<UserTypeData> getAllUserType() {
        List<UserTypeData> result = new ArrayList<>();
        result.add(new UserTypeData(USER_TYPE_ADMIN.getName(), USER_TYPE_ADMIN.getFormat()));
        result.add(new UserTypeData(USER_TYPE_TEACHER.getName(), USER_TYPE_TEACHER.getFormat()));
        result.add(new UserTypeData(USER_TYPE_STUDENT.getName(), USER_TYPE_STUDENT.getFormat()));
        return result;
    }
}
